package com.xjtu.se.homework.client.userInfo;

import com.xjtu.se.homework.common.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user_info")
public class UserInfoAPI {
    @Resource
    private UserInfoService service;

    @PostMapping("/get")
    public Result getUserInfo(@RequestParam("u_id") String u_id) {
        if (isUId(u_id)) {
            return Result.success("success", service.getUserInfo(u_id));
        } else {
            return Result.fail(500, "not a valid user id");
        }
    }

    private boolean isUId(String uId) {
        return service.searchId(uId);
    }

    @PostMapping("/mod/name")
    public Result modName(@RequestParam("u_id") String uId, @RequestParam("u_name") String u_name) {
        boolean Ok = service.modName(uId, u_name);
        if (Ok) {
            return Result.success(200);
        } else {
            return Result.fail(500, "fail to mod userInfo");
        }
    }

    @PostMapping("/mod/role")
    public Result modRole(@RequestParam("u_id") String u_id, @RequestParam("grade") String grade, @RequestParam("role_id") int role_id) {
        boolean Ok = service.modRole(u_id, role_id, grade);
        if (Ok) {
            return Result.success(200);
        } else {
            return Result.fail(500, "fail to mod userInfo");
        }
    }


    @PostMapping("/avatar/upload")
    public Result upLoad() {
        return Result.success(200);
    }

    @PostMapping("/avatar/mod")
    public Result modAvatar() {
        return Result.success(200);
    }

}